package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.*;
import one.stand.request.*;
import one.stand.response.UserLevelResponse;
import one.stand.service.BaseService;
import one.stand.service.DistributionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zrh
 * @CreateDate: 2019/5/24 17:18
 */
@Service
public class DistributionAccountServiceImpl extends BaseService implements DistributionService {


    @Override
    public ResultModel<Map<String, Object>> getRoyaltyData(RoyaltyRecordRequest request) {
        if(request == null || request.getIsAgent() == null || request.getCId() == null){
            return ResultModel.checkParamFail();
        }
        Map<String, Object> map = new HashMap<>(4);
        // 查询分销数据
        Map<String, Object> royaltyData = royaltyRecordMapper.getRoyaltyData(new RoyaltyRecordModel(){{
            if(request.getIsAgent() == 0){
                setCompanyId(request.getCId().intValue());
            }else{
                setCompanyKey(request.getCompanyKey());
            }
            setIsAgent(request.getIsAgent());
        }});
        // 查询公司佣金提成列表
        List<CompanyRoyaltyModel> royaltylist = companyRoyaltyMapper.select(new CompanyRoyaltyModel(){{
            setCompanyId(request.getCId().intValue());
        }});
        map.put("royaltylist",royaltylist);
        // 近7天付款订单金额
        List<OrdersModel> payOrderlist = ordersMapper.payOrderlist(new OrdersModel(){{
            if(request.getIsAgent() == 0){
                setCompanyId(request.getCId().intValue());
            }else{
                setCompanyKey(request.getCompanyKey());
            }
            setIsAgent(request.getIsAgent());
        }});
        // 近7天总订单数
        List<OrdersModel> allOrderAmountlist = ordersMapper.allOrderAmountlist(new OrdersModel(){{
            if(request.getIsAgent() == 0){
                setCompanyId(request.getCId().intValue());
            }else{
                setCompanyKey(request.getCompanyKey());
            }
        }});
        map.put("royaltyData",royaltyData);
        map.put("payOrderlist",payOrderlist);
        map.put("allOrderAmountlist",allOrderAmountlist);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Boolean> companyRoyalty(CompanyRoyaltyRequest request) {
        log.info("设置分销金额比例:{}", request.toString());
        if(request == null || request.getLevel() == null || request.getRoyaltyRatio() == null){
            return ResultModel.checkParamFail();
        }
        if(request.getRoyaltyRatio() != null && request.getRoyaltyRatio() > maxRatio){
            return ResultModel.fail("佣金比例最高不能超过" + maxRatio);
        }
        CompanyRoyaltyModel model = new CompanyRoyaltyModel();
        model.setCompanyId(request.getCId().intValue());
        model.setLevel(request.getLevel());
        model.setRoyaltyRatio(request.getRoyaltyRatio());
        if(request.getRoyaltyId() != null){
            model.setRoyaltyId(request.getRoyaltyId());
            companyRoyaltyMapper.updateByPrimaryKeySelective(model);
            return ResultModel.success(true);
        }
        model.setIsDel(0);
        model.setCreateTime(currentDate());
        companyRoyaltyMapper.insertSelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Boolean> insertCompanyRoyalty(CompanyRoyaltyRequest request) {
        log.info("设置分销金额比例:{}", request.toString());
        if(request == null || request.getCId() == null || request.getLevel() == null){
            return ResultModel.checkParamFail();
        }
        if(request.getOneRoyalty() == null || request.getOneRoyalty() <= 0){
            return ResultModel.fail("请输入一级分销比例~！");
        }
        if(request.getTwoRoyalty() == null || request.getTwoRoyalty() <= 0){
            return ResultModel.fail("请输入二级分销比例~！");
        }
        if((request.getOneRoyalty() + request.getTwoRoyalty()) > maxRatio){
            return ResultModel.fail("佣金比例最高不能超过" + maxRatio + "%");
        }
        if(request.getConditions() == null || request.getConditions() <= 0){
            return ResultModel.fail("请输入分销员升级条件~！");
        }
        CompanyRoyaltyModel model = new CompanyRoyaltyModel();
        model.setCompanyId(request.getCId().intValue());
        model.setLevel(request.getLevel());
        model.setOneRoyalty(request.getOneRoyalty());
        model.setTwoRoyalty(request.getTwoRoyalty());
        model.setConditions(request.getConditions());
        if(request.getRoyaltyId() != null){
            model.setRoyaltyId(request.getRoyaltyId());
            companyRoyaltyMapper.updateByPrimaryKeySelective(model);
            return ResultModel.success(true);
        }
        model.setIsDel(0);
        model.setCreateTime(currentDate());
        companyRoyaltyMapper.insertSelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Map<String, Object>> selectRoyaltyOrderList(OrdersRequest request) {
        if(request == null){
            return ResultModel.checkParamFail();
        }
        OrdersModel model = new OrdersModel();
        model.setCompanyId(request.getCId().intValue());
        model.setOrderState(request.getOrderState());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<OrdersModel> list = ordersMapper.selectRoyaltyOrderList(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        for(OrdersModel ordersModel : list){
            ordersModel.setName(StringUtils.isBlank(ordersModel.getName()) ? "" : new String(java.util.Base64.getDecoder().decode(ordersModel.getName())));
        }
        PageInfo<OrdersModel> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo",pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<OrdersModel> getRoyaltyOrderInfo(OrdersRequest request) {
        if(request == null || request.getOrderInfoId() == null){
            return ResultModel.checkParamFail();
        }
        OrdersModel model = new OrdersModel();
        model.setOrderInfoId(request.getOrderInfoId());
        OrdersModel ordersModel = ordersMapper.selectRoyaltyOrderInfo(model);
        ordersModel.setName(StringUtils.isBlank(ordersModel.getName()) ? "" : new String(java.util.Base64.getDecoder().decode(ordersModel.getName())));
        return ResultModel.success(ordersModel);
    }

    @Override
    public ResultModel<Map<String, Object>> selectRoyaltyRankList(RoyaltyRecordRequest request) {
        if(request == null || request.getDateType() == 1){
            return ResultModel.checkParamFail();
        }
        RoyaltyRecordModel model = new RoyaltyRecordModel();
        if(request.getIsAgent() == 0){
            model.setCompanyId(request.getCId().intValue());
        }else{
            model.setCompanyKey(request.getCompanyKey());
        }
        model.setDateType(request.getDateType());
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        List<RoyaltyRecordModel> list = royaltyRecordMapper.selectRoyaltyRankList(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        list.forEach(obj->{{
            obj.setName(StringUtils.isBlank(obj.getName()) ? "" : new String(java.util.Base64.getDecoder().decode(obj.getName())));
        }});
        PageInfo<RoyaltyRecordModel> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>(1);
        map.put("pageInfo",pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> selectCompanyTransferList(CompanyTransferRequest request) {
        log.info("企业到账记录列表:{}",JSON.toJSONString(request));
        if(request == null){
            return ResultModel.checkParamFail();
        }
        CompanyTransferModel model = new CompanyTransferModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId().intValue());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyTransferModel> list = companyTransferMapper.select(model);
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        for(CompanyTransferModel model1 : list){
            model1.setNickeName(StringUtils.isBlank(model1.getNickeName()) ? "" : new String(java.util.Base64.getDecoder().decode(model1.getNickeName())));
        }
        PageInfo<CompanyTransferModel> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = companyTransferMapper.getMoneyBackData(request.getCId().intValue());
        map.put("pageInfo", pageInfo);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Boolean> updateCompanyTransferState(CompanyTransferRequest request) {
        log.info("更改企业到账记录结算状态:{}",JSON.toJSONString(request));
        if(request == null || request.getTransferId() == null){
            return ResultModel.checkParamFail();
        }
        CompanyTransferModel model = new CompanyTransferModel();
        model.setTransferId(request.getTransferId());
        model.setState(2);
        companyTransferMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Boolean> updateRoyaltyRecordState(RoyaltyRecordRequest request) {
        log.info("更改佣金列表结算状态:{}",JSON.toJSONString(request));
        if(request == null || request.getRecordId() == null){
            return ResultModel.checkParamFail();
        }
        RoyaltyRecordModel model = new RoyaltyRecordModel();
        model.setRecordId(request.getRecordId());
        model.setState(2);
        royaltyRecordMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Map<String, Object>> selectUserLevelList(UserLevelRequest request) {
        log.info("查询公司分销员列表:{}",JSON.toJSONString(request));
        if(request == null || request.getCId() == null){
            return ResultModel.checkParamFail();
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        UserLevelModel model = new UserLevelModel();
        model.setCompanyId(request.getCId().intValue());

        Map<String, Object> map = new HashMap<>(1);
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<UserLevelResponse> getUserLevelInfo(UserLevelRequest request) {
        log.info("得到公司分销员信息:{}",JSON.toJSONString(request));
        if(request == null || request.getLevelId() == null){
            return ResultModel.checkParamFail();
        }
        UserLevelModel model = new UserLevelModel();
        model.setLevelId(request.getLevelId());
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> updateUserLevelState(UserLevelRequest request) {
        log.info("修改公司分销员状态:{}",JSON.toJSONString(request));
        if(request == null || request.getLevelId() == null || request.getState() == null){
            return ResultModel.checkParamFail();
        }
        UserLevelModel model = new UserLevelModel();
        model.setLevelId(request.getLevelId());
        model.setState(request.getState());
        userLevelMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }
}
